// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import jQuery from 'jquery'
import MintUi, { DatetimePicker, Loadmore, Spinner, Indicator } from 'mint-ui'
import App from './App'
import router from './router'
import {FCY_KEY} from './assets/js/config'
import LocalCache from './assets/js/cache'
import 'mint-ui/lib/style.css'
Vue.config.productionTip = false
Vue.use(MintUi, DatetimePicker.name, DatetimePicker, Loadmore.name, Loadmore, Spinner.name, Spinner, Indicator)
import Vee from 'vee-validate'
const veeConfig = {
  // errorBagName: 'errors',
  // delay: 0,
  // message: null,
  // strict: true,
  locale: 'zh_CN'
}
Vue.use(Vee, veeConfig)
const $ = jQuery
/* eslint-disable no-new */
Vue.filter('date', function (d) {
  try {
    if (d instanceof Date || new Date(d) instanceof Date) {
      let year = d.getFullYear()
      let month = d.getMonth() + 1
      month = month < 10 ? ('0' + month) : month
      let date = d.getDate()
      date = date < 10 ? ('0' + date) : date
      return year + '-' + month + '-' + date
    } else {
      return d
    }
  } catch (e) {
    return d
  }
})
window.myVue = new Vue({
  el: '#app',
  router,
  template: '<App/>',
  created () {
    Vue.prototype.post = (opt) => {
      const self = this
      $.ajax({
        url: opt.url,
        method: 'POST',
        data: opt.sendData,
        contentType: 'application/x-www-form-urlencoded;charset=utf-8',
        dataType: 'json',
        timeout: 30000,
        beforeSend (xhr) {
          typeof opt.beforeSend === 'function' ? opt.beforeSend(xhr) : null
        },
        success (data) {
          if (typeof data === 'string') {
            data = JSON.parse(data)
          }
          const code = parseInt(data.code)
          if (code === 0) {
            const newData = data.res.data
            typeof opt.success === 'function' ? opt.success(newData) : null
          } else if (code === 101 || code === 102) {
            self.$toast({
              message: '权限已过期',
              duration: 1000
            })
            LocalCache.del(FCY_KEY)
            self.$router.push({ path: '/' })
          } else {
            self.$toast({
              message: data.res.msg,
              duration: 1000
            })
          }
        },
        error (x, h, r) {
          self.$toast({
            message: '网络不畅，请稍后重试',
            duration: 2000
          })
        }
      })
    }
  },
  methods: {
  },
  components: { App }
})
